package com.ruoyi.inventory.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.goods.domain.TGoodsInfo;
import com.ruoyi.goods.service.ITGoodsInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.WCheckGoodsRel;
import com.ruoyi.inventory.service.IWCheckGoodsRelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 盘库关联Controller
 * 
 * @author wzy
 * @date 2024-08-11 12:51:49
 */
@RestController
@RequestMapping("/inventory/relate")
public class WCheckGoodsRelController extends BaseController
{
    @Autowired
    private IWCheckGoodsRelService wCheckGoodsRelService;

    @Autowired
    private ITGoodsInfoService tGoodsInfoService;

    private static Logger logger = LoggerFactory.getLogger(WCheckGoodsRelController.class);

    /**
     * 查询盘库关联列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:relate:list')")
    @GetMapping("/list")
    public TableDataInfo list(WCheckGoodsRel wCheckGoodsRel)
    {
        startPage();
        List<WCheckGoodsRel> list = wCheckGoodsRelService.selectWCheckGoodsRelList(wCheckGoodsRel);
        return getDataTable(list);
    }

    /**
     * 导出盘库关联列表
     */
    @PreAuthorize("@ss.hasPermi('inventory:relate:export')")
    @Log(title = "盘库关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WCheckGoodsRel wCheckGoodsRel)
    {
        List<WCheckGoodsRel> list = wCheckGoodsRelService.selectWCheckGoodsRelList(wCheckGoodsRel);
        ExcelUtil<WCheckGoodsRel> util = new ExcelUtil<WCheckGoodsRel>(WCheckGoodsRel.class);
        util.exportExcel(response, list, "盘库关联数据");
    }

    /**
     * 获取盘库关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('inventory:relate:query')")
    @GetMapping(value = "/{checkGoodsId}")
    public AjaxResult getInfo(@PathVariable("checkGoodsId") Long checkGoodsId)
    {
        return success(wCheckGoodsRelService.selectWCheckGoodsRelByCheckGoodsId(checkGoodsId));
    }

    /**
     * 新增盘库关联
     */
    @PreAuthorize("@ss.hasPermi('inventory:relate:add')")
    @Log(title = "盘库关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WCheckGoodsRel wCheckGoodsRel)
    {
        wCheckGoodsRel.setCreateBy(getNickName());
        return toAjax(wCheckGoodsRelService.insertWCheckGoodsRel(wCheckGoodsRel));
    }

    /**
     * 修改盘库关联
     */
    @PreAuthorize("@ss.hasPermi('inventory:relate:edit')")
    @Log(title = "盘库关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WCheckGoodsRel wCheckGoodsRel)
    {
        wCheckGoodsRel.setUpdateBy(getNickName());
        return toAjax(wCheckGoodsRelService.updateWCheckGoodsRel(wCheckGoodsRel));
    }

    /**
     * 删除盘库关联
     */
    @PreAuthorize("@ss.hasPermi('inventory:relate:remove')")
    @Log(title = "盘库关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{checkGoodsIds}")
    public AjaxResult remove(@PathVariable Long[] checkGoodsIds)
    {
        return toAjax(wCheckGoodsRelService.deleteWCheckGoodsRelByCheckGoodsIds(checkGoodsIds));
    }

    /**
     * 新增盘库关联
     */
    @PreAuthorize("@ss.hasPermi('inventory:relate:add')")
    @Log(title = "盘库关联", businessType = BusinessType.INSERT)
    @PostMapping("/ext/{checkGoodsId}")
    public AjaxResult addExt(@RequestBody TGoodsInfo tGoodsInfo, @PathVariable("checkGoodsId") Long checkGoodsId)
    {
        //logger.debug("YYY " + "checkGoodsId = " + checkGoodsId);
        //logger.debug("YYY " + "tGoodsInfo = " + tGoodsInfo);
        String bundleNumber = tGoodsInfo.getBundleNumber();
        if(StringUtils.isNotEmpty(bundleNumber)) {
            tGoodsInfo.setBundleNumber(bundleNumber.replace(",", "|"));
        }
        return toAjax(wCheckGoodsRelService.insertWCheckGoodsRelExt(tGoodsInfo, checkGoodsId));
        //return toAjax(true);
    }

    /**
     * 盘库操作接口，盘库操作
     */
    @PreAuthorize("@ss.hasPermi('inventory:relate:check')")
    @Log(title = "盘库操作", businessType = BusinessType.CHECK)
    @PostMapping("/check/{checkGoodsId}")
    public AjaxResult check(@RequestBody TGoodsInfo tGoodsInfo, @PathVariable("checkGoodsId") Long checkGoodsId)
    {
        //logger.debug("WWW " + "tGoodsInfo = " + tGoodsInfo);
        logger.debug("WWW " + "checkGoodsId = " + checkGoodsId);
        String bundleNumber = tGoodsInfo.getBundleNumber();
        WCheckGoodsRel wCheckGoodsRel = new WCheckGoodsRel();
        wCheckGoodsRel.setCheckGoodsId(checkGoodsId);
        wCheckGoodsRel.setBundleNumber(bundleNumber);

        WCheckGoodsRel rCheckGoodsRel = wCheckGoodsRelService.selectWCheckGoodsRelByCheckGoodsRel(wCheckGoodsRel);
        if(StringUtils.isNotNull(rCheckGoodsRel)) {
            rCheckGoodsRel.setBundleNumber(bundleNumber);
            rCheckGoodsRel.setCheckGoodsStatus("已盘");
            rCheckGoodsRel.setCheckGoodsPerson(getNickName());
            rCheckGoodsRel.setCheckGoodsTime(DateUtils.getNowDate());
            if(wCheckGoodsRelService.updateWCheckGoodsRel(rCheckGoodsRel) > 0) {
                return success("O", bundleNumber);
            } else {
                return error("F", bundleNumber);
            }
        } else {
            TGoodsInfo rGoodsInfo = tGoodsInfoService.selectTGoodsInfoByBundleNumber(bundleNumber);
            if(StringUtils.isNotNull(rGoodsInfo)) {
                return success("K", bundleNumber);
            } else {
                return success("N", bundleNumber);
            }
        }
        //return toAjax(wCheckGoodsRelService.checkGoodsByGoodsInfo(tGoodsInfo, checkGoodsId));
    }

    /**
     * 删除盘库关联
     */
    @PreAuthorize("@ss.hasPermi('inventory:relate:remove')")
    @Log(title = "盘库关联", businessType = BusinessType.REMOVE)
    @DeleteMapping("/ext/{goodsIds}/{checkGoodsId}")
    public AjaxResult removeExt(@PathVariable Long[] goodsIds, @PathVariable Long checkGoodsId)
    {
        //logger.debug("WWW " + "removeExt = " + checkGoodsId);
        return toAjax(wCheckGoodsRelService.deleteWCheckGoodsRelByCheckGoodsIdsExt(goodsIds, checkGoodsId));
    }

}
